<template>
  <div id="invSernumRecord" style="height:100%" v-loading="loading">
    <v-tableTitle :title="$t('ex.invSernumRecord')" :btnConfig="btnConfig" @download="download" :downloading="downloading"> </v-tableTitle>
    <GTable
      class="base-grid"
      :hasCheck="true"
      v-model="gridObj"
      api="invSernumRecord/selectInvSernumRecordList"
      :otherHeight="0"
      :tmsShowFilter="true"
      :columnConfig="columnConfig"
      @selection-change="handleSelectionChange"
      @filter-changed="filterChanged"
      @grid-ready="agGridReady"
    >
    </GTable>
  </div>
</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import agGridBaseMixin from "@/mixins/AgGridBaseMixin";

export default {
  name: "invSernumRecord",

  mixins: [baseMixin, agGridBaseMixin],
  props: {
    inputData: {
      type: Object,
    },
  },
  data() {
    return {
      height: 0,
      title:this.$t('ex.invSernumRecord'),
      columnConfig: {
        menu_code: "invSernumRecord",
        menu_id: 995,
        menu_list_code: "invSernumRecord",
      },
      dialogFlag: false,
      downloading:false,
      selectData: [],
    };
  },
  computed: {
    btnConfig() {
      return [{ label: this.$t('common.export'), certigier: !this.btnPermNameList.includes('WM06020901'), invalid: "download",loading:this.downloading}];
    },
  },
  watch: {
    $route(data) {
      if (data.name === "invSernumRecord") {
        this.dialogFlag = false;
      }
    },
  },
  methods: {
    filterChanged(param){
      this.filterParams = param.api.getFilterModel()
    },
    handleSelectionChange(data) {
      this.selectData = data;
    },
    download(){
      const {menu_list_code:menuListCode,menu_code:menuCode} = this.columnConfig;
      const excelName = this.title;
      if(menuListCode&&menuCode&&excelName){
        let params = []
        if(!this._.isEmpty(this.filterParams)){
          let obj = {}
          Object.keys(this.filterParams).forEach(key => {
            obj[key] = this.filterParams[key].filter
          })
          params = [obj]
        }
        if(!this._.isEmpty(this.selectData)){
          params = this.selectData
        }
        this.downloading = true
        this.$download({menuCode,menuListCode,name:excelName},params).then(res => {
          this.downloading = false;
        }).finally(() => {
          this.downloading = false;
        })
      }
    },
  },
  mounted() {
    this.$store.commit("setSkip", {});
    this.$store.commit("setRouteId", "");
    this.$store.commit("setNewFlag", false);
    this.$store.commit("setRouteData", {});
  },
};
</script>

<style scoped>
#invSernumRecord {
  width: 100%;
  height: 99%;
}
</style>
